﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using VisionRoc.Core;

namespace VisionRoc_Measure
{
    public partial class FormRotateTool : Form
    {
        RotateCorrectAlgo MyrotateCorrectAlgo;
        public FormRotateTool(RotateCorrectAlgo rotate)
        {
            InitializeComponent();
            MyrotateCorrectAlgo = rotate;
        }

        private void FormRotateTool_Load(object sender, EventArgs e)
        {
            try
            {
                MyrotateCorrectAlgo.ReadParameter();
                if (MyrotateCorrectAlgo.InIState == 1)
                {
                    txt_NineFilePath.Text = MyrotateCorrectAlgo.NineCalibrationFilePath.ToString();
                    txt_RotateFilePath.Text = MyrotateCorrectAlgo.RotateCalibrationFilePath.ToString();
                    txt_RotateCenterX.Text = MyrotateCorrectAlgo.RotateCenterX.ToString();
                    txt_RotateCenterY.Text = MyrotateCorrectAlgo.RotateCenterY.ToString();
                    txt_RotateCenterRadius.Text = MyrotateCorrectAlgo.RotateRadius.ToString();
                    txt_ModelX.Text = MyrotateCorrectAlgo.ModelX.ToString();
                    txt_ModelY.Text = MyrotateCorrectAlgo.ModelY.ToString();
                    txt_ModelR.Text = MyrotateCorrectAlgo.ModelR.ToString();
                    if (MyrotateCorrectAlgo.AngleDiffSet == 1)
                    {
                        radioButton_AngleDiff1.Checked = true;
                    }
                    else
                    {
                        radioButton_AngleDiff1.Checked = false;
                    }

                    if (MyrotateCorrectAlgo.ResuleAngleIsOrReverse == 2)
                    {
                        checkBox_IsOrReverse.Checked = true;
                    }
                    else
                    {
                        checkBox_IsOrReverse.Checked = false;
                    }
                    tooldisplay.Text = "标定文件加载成功!";
                    tooldisplay.ForeColor = Color.Green;
                }
                else
                {
                    txt_RotateCenterX.Text = "0";
                    txt_RotateCenterY.Text = "0";
                    txt_RotateCenterRadius.Text = "0";
                    txt_ModelX.Text = "0";
                    txt_ModelY.Text = "0";
                    txt_ModelR.Text = "0";
                    checkBox_IsOrReverse.Checked = true;
                    tooldisplay.Text = "标定文件加载失败";
                    tooldisplay.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                txt_RotateCenterX.Text = "0";
                txt_RotateCenterY.Text = "0";
                txt_RotateCenterRadius.Text = "0";
                txt_ModelX.Text = "0";
                txt_ModelY.Text = "0";
                txt_ModelR.Text = "0";
                checkBox_IsOrReverse.Checked = true;
                tooldisplay.Text = "标定文件加载失败";
                tooldisplay.ForeColor = Color.Red;
                MessageBox.Show("加载异常 :" + ex.ToString());
            }

        }

        private void btn_select_ninefile_Click_1(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = false;
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                openFileDialog.Filter = "九点标定文件XML|*.xml";
                openFileDialog.Title = "导入标定文件XML";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MyrotateCorrectAlgo.ReafCalibrationXML(openFileDialog.FileName);
                    //MyrotateCorrectAlgo.HomMat2D = RotateCalculateAlgo.GenHomMat2D(MyrotateCorrectAlgo.m_CalibrationData.SX, MyrotateCorrectAlgo.m_CalibrationData.SY, MyrotateCorrectAlgo.m_CalibrationData.Theta, MyrotateCorrectAlgo.m_CalibrationData.Phi, MyrotateCorrectAlgo.m_CalibrationData.TX, MyrotateCorrectAlgo.m_CalibrationData.TY);
                    string filename = txt_NineFilePath.Text = openFileDialog.FileName;
                    tooldisplay.Text = "九点标定文件XML加载成功!";
                    tooldisplay.ForeColor = Color.Green;
                }
            }
            catch (Exception ex)
            {
                tooldisplay.Text = "九点标定文件XML加载异常";
                tooldisplay.ForeColor = Color.Red;
                MessageBox.Show("九点标定文件XML加载异常 :" + ex.ToString());
            }

        }

        private void btn_select_rotatefile_Click_1(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = false;
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                openFileDialog.Filter = "旋转标定文件XML|*.xml";
                openFileDialog.Title = "导入标定文件XML";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filename = txt_RotateFilePath.Text = openFileDialog.FileName;
                    MyrotateCorrectAlgo.ReafCalibrationXML(filename);
                    MyrotateCorrectAlgo.RotateCenterX = Math.Round(MyrotateCorrectAlgo.m_CalibrationData.ImageRotatoCenterX, 3);
                    MyrotateCorrectAlgo.RotateCenterY = Math.Round(MyrotateCorrectAlgo.m_CalibrationData.ImageRotatoCenterY, 3);
                    MyrotateCorrectAlgo.RotateRadius = Math.Round(MyrotateCorrectAlgo.m_CalibrationData.ImageRotatoRadius, 3);

                    txt_RotateCenterX.Text = MyrotateCorrectAlgo.RotateCenterX.ToString();
                    txt_RotateCenterY.Text = MyrotateCorrectAlgo.RotateCenterY.ToString();
                    txt_RotateCenterRadius.Text = MyrotateCorrectAlgo.RotateRadius.ToString();


                    tooldisplay.Text = "旋转标定文件XML加载成功!";
                    tooldisplay.ForeColor = Color.Green;
                }
            }
            catch (Exception ex)
            {
                tooldisplay.Text = "旋转标定文件XML加载异常";
                tooldisplay.ForeColor = Color.Red;
                MessageBox.Show("旋转标定文件XML加载异常 :" + ex.ToString());
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                MyrotateCorrectAlgo.NineCalibrationFilePath = txt_NineFilePath.Text;
                MyrotateCorrectAlgo.RotateCalibrationFilePath = txt_RotateFilePath.Text;
                MyrotateCorrectAlgo.ModelX = Convert.ToDouble(txt_ModelX.Text);
                MyrotateCorrectAlgo.ModelY = Convert.ToDouble(txt_ModelY.Text);
                MyrotateCorrectAlgo.ModelR = Convert.ToDouble(txt_ModelR.Text);
                if (radioButton_AngleDiff1.Checked)
                {
                    MyrotateCorrectAlgo.AngleDiffSet = 1;
                }
                else
                {
                    MyrotateCorrectAlgo.AngleDiffSet = 2;
                }

                if (checkBox_IsOrReverse.Checked)
                {
                    MyrotateCorrectAlgo.ResuleAngleIsOrReverse = 2;
                }
                else
                {
                    MyrotateCorrectAlgo.ResuleAngleIsOrReverse = 1;
                }

                MyrotateCorrectAlgo.WriteParameter();
                tooldisplay.Text = "保存成功";
                tooldisplay.ForeColor = Color.Green;
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                tooldisplay.Text = "保存失败";
                tooldisplay.ForeColor = Color.Red;
                MessageBox.Show("参数保存失败 ：" + ex.ToString());
            }

        }
    }
}
